package com.ideabove.heart.util;

import java.util.Random;

import android.graphics.PorterDuff.Mode;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.TextView;

public class MyUtil {

	public interface ImageTouch{
		public void touch(View v, MotionEvent event);
	}
	
	public static int getRandomNumber(int min, int max){
		Random r = new Random();
		return r.nextInt(max - min + 1) + min;
	}

	public static void touchMe(View v, MotionEvent event, ImageTouch touch){

		if(v == null){
			if(event.getAction() == MotionEvent.ACTION_UP){
				touch.touch(v,event);
			}
		}else{
			ImageView view = (ImageView)v;
			if(event.getAction() == MotionEvent.ACTION_DOWN){

				view.setPadding(2, 2, 2, 2);
				view.setColorFilter(0xaa111111,Mode.SRC_OVER);				
			}else if(event.getAction() == MotionEvent.ACTION_UP){

				touch.touch(v,event);

				view.setPadding(0, 0, 0, 0);
				view.setColorFilter(0x00000000, Mode.SRC_OVER);
			}else{
				view.setPadding(0, 0, 0, 0);
				view.setColorFilter(0x00000000, Mode.SRC_OVER);
			}
		}
	}

	public static void blinkText(final TextView text_to_animate, int durationMillis) {

		final AlphaAnimation fade_out = new AlphaAnimation(1.0f, 0.0f);
		//ScaleAnimation scale_it = new ScaleAnimation(1.0f, 1.25f, 1.0f, 1.25f);
		fade_out.setDuration(durationMillis);

		final AlphaAnimation fade_in = new AlphaAnimation(0.0f, 1.0f);
		//ScaleAnimation scale_it = new ScaleAnimation(1.0f, 1.25f, 1.0f, 1.25f);
		fade_in.setDuration(durationMillis);

		fade_out.setAnimationListener(new AnimationListener() {
			public void onAnimationEnd(Animation arg0) {
				// TODO Auto-generated method stub
				//				if (recording == true)
				text_to_animate.startAnimation(fade_in);
			}
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub              
			}

			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub              
			}

		});

		fade_in.setAnimationListener(new AnimationListener() {
			public void onAnimationEnd(Animation arg0) {
				// TODO Auto-generated method stub
				//				if (recording == true)
				text_to_animate.startAnimation(fade_out);
			}
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub              
			}

			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub              
			}

		});

		text_to_animate.startAnimation(fade_out);       

	}
}
